<?php
/*
*
* workflow.service  模型
*
* ----------------------------------------------
* 零起飞网络 - 专注于企业管理系统开发
* 以质量求生存，以服务谋发展，以信誉创品牌 !
* ----------------------------------------------
* @copyright Copyright (C) 2017-2022 07FLY Network Technology Co,LTD.
* @license For licensing, see LICENSE.html or http://www.07fly.xyz/html/business
* @author ：kfrs <goodkfrs@QQ.com> 574249366
* @version ：1.0
* @link ：http://www.07fly.xyz
*/

namespace app\workflow\service;

use think\Db;

/**
 * 模块基类
 */
class WorkflowBusiness extends WorkflowBase
{

    /**
     *流程关联业务=》根据类型=》转换为业务数据列表
     * @param string $type
     * @return mixed
     * Author: kfrs <goodkfrs@QQ.com> created by at 2020/10/29 0029
     */
    public function getBusinessTypeInfo($business_type, $business_id)
    {
        //业务类型
        switch ($business_type) {
            //请假
            case 'oa_leave':
                if (tableExists('oa_leave')) {
                    $data = Db::name('oa_leave')->where(['id' => $business_id])->find();
                    if ($data) {
                        $data['type_name'] = $this->modelOaLeave->type($data['type'])['name'];
                        $data['create_user_name'] = $this->modelSysUser->getValue(['id' => $data['create_user_id']], 'realname');
                        $data['summary'] = '[' . $data['type_name'] . '] ' . $data['start_time'] . '到' . $data['end_time'];//摘要
                        $data['summary_status'] = $this->getBusinessTypeInfoStatus($data['status'])['name'];
                        $data['summary_html'] = $this->getBusinessTypeInfoHtml($business_type, $data);
                    }
                }
                break;
            //出差
            case 'oa_business_trip':
                if (tableExists('oa_business_trip')) {
                    $data = Db::name('oa_business_trip')->where(['id' => $business_id])->find();
                    if ($data) {
                        $data['create_user_name'] = $this->modelSysUser->getValue(['id' => $data['create_user_id']], 'realname');
                        $data['summary'] = '地点：' . $data['address'] . ' 日期:' . $data['start_time'] . '到' . $data['end_time'];//摘要
                        $data['summary_status'] = $this->getBusinessTypeInfoStatus($data['status'])['name'];
                        $data['summary_html'] = $this->getBusinessTypeInfoHtml($business_type, $data);
                    }
                }
                break;
            //借款申请
            case 'oa_borrow':
                if (tableExists('oa_borrow')) {
                    $data = Db::name('oa_borrow')->where(['id' => $business_id])->find();
                    if ($data) {
                        $data['create_user_name'] = $this->modelSysUser->getValue(['id' => $data['create_user_id']], 'realname');
                        $data['summary'] = '[借款申请] 金额' . $data['money'];//摘要
                        $data['summary_status'] = $this->getBusinessTypeInfoStatus($data['status'])['name'];
                        $data['summary_html'] = $this->getBusinessTypeInfoHtml($business_type, $data);
                    }
                }
                break;
            //工作请示
            case 'oa_consult':
                if (tableExists('oa_consult')) {
                    $data = Db::name('oa_consult')->where(['id' => $business_id])->find();
                    if ($data) {
                        $data['level_name'] = $this->modelOaConsult->level($data['level'])['name'];//级别
                        $data['create_user_name'] = $this->modelSysUser->getValue(['id' => $data['create_user_id']], 'realname');
                        $data['summary'] = '工作请示=》[' . $data['level_name'] . '] ' . $data['content'];//摘要
                        $data['summary_status'] = $this->getBusinessTypeInfoStatus($data['status'])['name'];
                        $data['summary_html'] = $this->getBusinessTypeInfoHtml($business_type, $data);
                    }
                }
                break;
            //报销申请
            case 'oa_reimburse':
                if (tableExists('oa_reimburse')) {
                    $data = Db::name('oa_reimburse')->where(['id' => $business_id])->find();
                    if ($data) {
                        $data['reimburse_list'] = $this->modelOaReimburseList->getList(['reimburse_id' => $data['id']], '', '', false);
                        $data['create_user_name'] = $this->modelSysUser->getValue(['id' => $data['create_user_id']], 'realname');
                        $data['summary'] = '费用报销=》 [' . $data['name'] . '] 金额：' . $data['money'];//摘要
                        $data['summary_status'] = $this->getBusinessTypeInfoStatus($data['status'])['name'];
                        $data['summary_html'] = $this->getBusinessTypeInfoHtml($business_type, $data);
                    }
                }
                break;
            case 'sal_contract':
                if (tableExists('sal_contract')) {
                    $data = Db::name('sal_contract')->where(['id' => $business_id])->find();
                    if ($data) {
                        $data['customer_name'] = Db::name('cst_customer')->where(['id' => $data['customer_id']])->value('name');
                        $data['create_user_name'] = $this->modelSysUser->getValue(['id' => $data['create_user_id']], 'realname');
                        $data['summary'] = '合同名称：[' . $data['name'] . '] 销售金额：' . $data['money'];//摘要
                        $data['summary_status'] = $this->getBusinessTypeInfoStatus($data['audit_status'])['name'];
                        $data['summary_html'] = $this->getBusinessTypeInfoHtml($business_type, $data);
                    }
                }
                break;
            case 'pos_contract':
                if (tableExists('sal_contract')) {
                    $data = Db::name('pos_contract')->where(['id' => $business_id])->find();
                    if ($data) {
                        $data['create_user_name'] = $this->modelSysUser->getValue(['id' => $data['create_user_id']], 'realname');
                        $data['summary'] = '采购合同：[' . $data['name'] . '] 销售金额：' . $data['money'];//摘要
                        $data['summary_status'] = $this->getBusinessTypeInfoStatus($data['audit_status'])['name'];
                        $data['summary_html'] = $this->getBusinessTypeInfoHtml($business_type, $data);
                    }
                }
                break;
            //入库单
            case 'stock_into':
                if (tableExists('stock_into')) {
                    $data = Db::name('stock_into')->where(['id' => $business_id])->find();
                    if ($data) {
                        $data['create_user_name'] = $this->modelSysUser->getValue(['id' => $data['create_user_id']], 'realname');
                        $data['summary'] = '入库单：[' . $data['name'] . ']  单号：' . $data['into_no'];//摘要
                        $data['summary_status'] = $this->getBusinessTypeInfoStatus($data['audit_status'])['name'];
                        $data['summary_html'] = $this->getBusinessTypeInfoHtml($business_type, $data);
                    }
                }
                break;
                //出库单
            case 'stock_out':
                if (tableExists('stock_out')) {
                    $data = Db::name('stock_out')->where(['id' => $business_id])->find();
                    if ($data) {
                        $data['create_user_name'] = $this->modelSysUser->getValue(['id' => $data['create_user_id']], 'realname');
                        $data['summary'] = '出库单：[' . $data['name'] . ']  单号：' . $data['out_no'];//摘要
                        $data['summary_status'] = $this->getBusinessTypeInfoStatus($data['audit_status'])['name'];
                        $data['summary_html'] = $this->getBusinessTypeInfoHtml($business_type, $data);
                    }
                }
                break;
        }

        //接入办公审批流程=>自义表单
        if(strpos($business_type,'workflow_diy') !== false){
            if (tableExists($business_type)) {
                $info = Db::name($business_type)->where(['id' => $business_id])->find();
                $table = $this->modelWorkflowDiyForm->getInfo(['id' => $info['diy_form_id']], 'name,ext_table,main_table');

                //自定义字段，列表展示
                $field_list = $this->logicWorkflowDiyField->getFieldExtFields($info['diy_form_id'], 'visible');

                $html='<div class="row"><div class="col-sm-4">';
                foreach ($field_list as $row){
                    $html .='<p><strong>'.$row['show_name'].'</strong>：'.$info[$row["field_name"]].'</p>';
                }

                $html .='</div>';

                if ($info) {
                    $data['create_user_name'] = $this->modelSysUser->getValue(['id' => $info['create_user_id']], 'realname');
                    $data['summary'] = '[' . $table['name'] . ']  单号：' . $info['id'];//摘要
                    $data['summary_status'] = $this->getBusinessTypeInfoStatus($info['audit_status'])['name'];
                    $data['summary_html'] = $html;
                }
            }
        }

        //表示未找到业务数据
        if (empty($data)) {
            $data = [
                'summary' => '业务数据不存在',//摘要
                'summary_html' => '',       //摘要html文本
                'summary_status' => '',     //审核状态
                'create_user_name' => '',//创建人员
            ];
        }
        return $data;
    }

    /**
     * 流程关联业务=》设置更新业务表审批字段
     * @param $business_type
     * @param $business_id
     * @param $status 0=临时单，1=待审核，2=已通过，3=被否决，4=被驳回，5=已撤销
     * @return array
     * Author: kfrs <goodkfrs@QQ.com> created by at 2020/11/24 0024
     */
    public function uptBusinessTypeInfo($business_type, $business_id, $status)
    {

        switch ($business_type) {
            case 'oa_leave':
                if (tableExists('oa_leave')) {
                    Db::name('oa_leave')->where(['id' => $business_id])->update(['status' => $status]);
                }
                break;
            case 'oa_business_trip':
                if (tableExists('oa_business_trip')) {
                    Db::name('oa_business_trip')->where(['id' => $business_id])->update(['status' => $status]);
                }
                break;
            case 'oa_borrow':
                if (tableExists('oa_borrow')) {
                    Db::name('oa_borrow')->where(['id' => $business_id])->update(['status' => $status]);
                }
                break;
            case 'oa_consult':
                if (tableExists('oa_consult')) {
                    Db::name('oa_consult')->where(['id' => $business_id])->update(['status' => $status]);
                }
                break;
            case 'oa_reimburse':
                if (tableExists('oa_reimburse')) {
                    Db::name('oa_reimburse')->where(['id' => $business_id])->update(['status' => $status]);
                }
                break;

            case 'sal_contract':
                if (tableExists('sal_contract')) {
                    Db::name('sal_contract')->where(['id' => $business_id])->update(['audit_status' => $status]);

                    //d($status);
                    //当审批通过时，自动设置用户为保护状态
                    if ($status == 2) {
                        $info = Db::name('sal_contract')
                            ->where(['id' => $business_id])
                            ->field('customer_id,end_date')
                            ->find();;
                        //d($info);
                        if ($info) {
                            $updata['protect_status'] = 1;
                            $updata['protect_expire_date'] = $info['end_date'];
                            $updata['recover_start_date'] = format_time();
                            Db::name('cst_customer')->where(['id' => $info['customer_id']])->update($updata);

                        }
                    }
                }
            //采购合同
            case 'pos_contract':
                if (tableExists('pos_contract')) {
                    Db::name('pos_contract')->where(['id' => $business_id])->update(['audit_status' => $status]);
                }
                break;
            //入库单
            case 'stock_into':
                if (tableExists('stock_into')) {
                    Db::name('stock_into')->where(['id' => $business_id])->update(['audit_status' => $status]);
                }
                break;
            //出库单
            case 'stock_out':
                if (tableExists('stock_out')) {
                    Db::name('stock_out')->where(['id' => $business_id])->update(['audit_status' => $status]);
                }
                break;
        }

        //接入办公审批流程=>自义表单
        if(strpos($business_type,'workflow_diy') !== false){
            if (tableExists($business_type)) {
                if (tableExists($business_type)) {
                    Db::name($business_type)->where(['id' => $business_id])->update(['audit_status' => $status]);
                }
            }
        }

        return true;
    }

    /**
     * 获取业务详细表输出内容
     * @param array $data
     * Author: 开发人生 goodkfrs@qq.com
     * Date: 2022/2/18 0018 15:13
     */
    public function getBusinessTypeInfoHtml($business_type, $data = [])
    {
        $html = '';
        switch ($business_type) {
            case 'oa_leave':

                $html .= '<div class="row">';
                $html .= <<<INFO
<div class="col-sm-4">
<p><strong>请假人：</strong>{$data['create_user_name']}</p>
<p><strong>请假类型：</strong>{$data['type_name']}</p>
<p><strong>请假时间：</strong>{$data['start_time']} ~ {$data['end_time']} </p>
<p><strong>请假原因：</strong>{$data['reason']}</p>
</div>
INFO;

                $html .= '</div>';

                break;
            case 'oa_business_trip':

                $html .= '<div class="row">';
                $html .= <<<INFO
<div class="col-sm-4">
<p><strong>申请人员：</strong>{$data['create_user_name']}</p>
<p><strong>出差天数：</strong>{$data['days']}</p>
<p><strong>出差时间：</strong>{$data['start_time']} ~ {$data['end_time']} </p>
<p><strong>出差预算：</strong>{$data['money']}</p>
<p><strong>出差地点：</strong>{$data['address']}</p>
<p><strong>出差原因：</strong>{$data['reason']}</p>
</div>
INFO;

                $html .= '</div>';

                break;
            case 'oa_borrow':

                $html .= '<div class="row">';
                $html .= <<<INFO
<div class="col-sm-4">
<p><strong>申请人员：</strong>{$data['create_user_name']}</p>
<p><strong>申请金额：</strong>{$data['money']}</p>
<p><strong>申请原因：</strong>{$data['reason']}</p>
</div>
INFO;
                $html .= '</div>';

                break;
            case 'oa_consult':

                $html .= '<div class="row">';
                $html .= <<<INFO
<div class="col-sm-4">
<p><strong>申请人员：</strong>{$data['create_user_name']}</p>
<p><strong>紧急程度：</strong>{$data['level_name']}</p>
<p><strong>请求内容：</strong>{$data['content']}</p>
</div>
INFO;
                $html .= '</div>';

                break;
            case 'oa_reimburse':

                $reimburse_list = '';
                if (!empty($data['reimburse_list'])) {
                    $reimburse_list = '<table class="table-hover table">';
                    foreach ($data['reimburse_list'] as $row) {
                        $reimburse_list .= '<tr>';
                        $reimburse_list .= '<td>' . $row['name'] . '</td>';
                        $reimburse_list .= '<td>' . $row['money'] . '</td>';
                        $reimburse_list .= '<td>' . $row['type_name'] . '</td>';
                        $reimburse_list .= '<td>' . $row['handle_date'] . '</td>';
                        $reimburse_list .= '<td>' . $row['remark'] . '</td>';
                        $reimburse_list .= '<tr>';
                    }
                    $reimburse_list .= '</table>';
                }
                $html .= '<div class="row">';
                $html .= <<<INFO
<div class="col-sm-4">
<p><strong>报销人员：</strong>{$data['create_user_name']}</p>
<p><strong>报销金额：</strong>{$data['money']}</p>
</div>
<div class="col-sm-8">
<p><strong>报销明细：</strong></p>
<p><strong>{$reimburse_list}</p>
</div>
INFO;
                $html .= '</div>';

                break;

            case 'sal_contract':
                $html .= '<div class="row">';
                $html .= <<<INFO
<div class="col-sm-4">
<p><strong>合同标题：</strong>{$data['name']}</p>
<p><strong>合同金额：</strong>{$data['money']}</p>
<p><strong>去零金额：</strong>{$data['zero_money']}</p>
<p><strong>回款金额：</strong>{$data['back_money']}</p>
<p><strong>欠款金额：</strong>{$data['owe_money']}</p>
<p><strong>签订日期：</strong>{$data['start_date']}</p>
<p><strong>到期日期：</strong>{$data['end_date']}</p>
<p><strong>合同简述：</strong>{$data['remark']}</p>
<p><strong>审批结果：</strong>{$data['summary_status']}</p>
</div>

<div class="col-sm-4">
<p><strong>客户名称：</strong>{$data['customer_name']}</p>
<p><strong>合同类型：</strong>{$data['contract_type']}</p>
<p><strong>画面类型：</strong>{$data['images_type']}</p>
<p><strong>广告发布内容：</strong>{$data['adv_content']}</p>
<p><strong>媒体数量：</strong>{$data['media_num']}</p>
<p><strong>付款方式：</strong>{$data['payment_method']}</p>
<p><strong>特殊要求：</strong>{$data['special_req']}</p>
</div>

INFO;

                $html .= '</div>';

                break;

            case 'pos_contract':
                $html .= '<div class="row">';
                $html .= <<<INFO
<div class="col-sm-4">
<p><strong>合同编号：</strong>{$data['contract_no']}</p>
<p><strong>合同标题：</strong>{$data['name']}</p>
<p><strong>合同金额：</strong>{$data['money']}</p>
<p><strong>去零金额：</strong>{$data['zero_money']}</p>
<p><strong>付款金额：</strong>{$data['pay_money']}</p>
<p><strong>欠款金额：</strong>{$data['owe_money']}</p>
<p><strong>签订日期：</strong>{$data['purchase_date']}</p>
<p><strong>到货日期：</strong>{$data['arrive_date']}</p>
<p><strong>合同简述：</strong>{$data['remark']}</p>
<p><strong>审批结果：</strong>{$data['summary_status']}</p>
</div>

<div class="col-sm-4">

</div>

INFO;

                $html .= '</div>';

                break;
        }
        return $html;
    }

    /**
     * 业务处理结果状态
     * @param string $key
     * @return array|mixed
     * Author: 开发人生 goodkfrs@qq.com
     * Date: 2022/2/25 0025 16:45
     */
    public function getBusinessTypeInfoStatus($key = '')
    {
        $data = array(
            "0" => array(
                'name' => '待提交',
                'html' => '<span class="label">待提交<span>',
            ),
            "1" => array(
                'name' => '待审核',
                'html' => '<span class="label label-info">待审核<span>',
            ),
            "2" => array(
                'name' => '审核中',
                'html' => '<span class="label label-info">审核中<span>',
            ),
            "3" => array(
                'name' => '已通过',
                'html' => '<span class="label label-success">已通过<span>',
            ),
            "4" => array(
                'name' => '被驳回',
                'html' => '<span class="label label-danger">被驳回<span>',
            ),
            "5" => array(
                'name' => '被撤消',
                'html' => '<span class="label label-default">被撤消<span>',
            ),
        );
        return (array_key_exists($key, $data)) ? $data[$key] : $data;
    }

}

?>